package com.whiteape.book.model.dto;


import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.ToOne;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Transient;

import com.whiteape.book.model.gen.DaoSession;
import com.whiteape.book.model.gen.MediaChapterBeanDao;
import com.whiteape.book.model.gen.SiteSourceBeanDao;
import com.whiteape.book.model.gen.CollectionMediaBeanDao;
import com.whiteape.book.ui.base.BaseRVFragment;
import com.whiteape.book.utils.GsonUtils;
import com.whiteape.book.utils.MapConverter;

@Entity
public class CollectionMediaBean implements Parcelable, BaseRVFragment.Movable{


    //网络url或者本地书籍的md5
    @Id
    private String id;

    @NotNull
    private Integer mediaType;
    @NotNull
    private String title;
    private String author;
    private String coverUrl;
    private String desc;
    private String wordCount;
    private String state;
    private String category;
    private String lastChapter;
    private String lastUpdated;
    //最新阅读日期
    private String lastRead;

    //总章节数
    private Integer chaptersCount;
    //当前阅读章节
    private Integer currentChapter;
    //当前阅读章节的阅读页数
    private Integer currentPage;

    //是否有更新或未阅读
    private Boolean ifUpdated = true;
    //是否是本地书籍
    private Boolean ifLocal = false;

    //书源url
    private String bookSourceUrl;

    //1.1.5新增 分组 + 自定义内容 + 加密
    private String group;

    private String diyTitle;

    private String diyAuthor;

    private String diyDesc;

    private String diyCoverUrl;

    private Boolean encrypted=false;


    @Override
    public boolean canMove() {
        return true;
    }

    /**
     * 动态参数
     */
//    private String varMapStr;
    @Convert(columnType = String.class, converter = MapConverter.class)
    private HashMap<String, String> varMap;

    @ToOne(joinProperty = "bookSourceUrl")
    private SiteSourceBean siteSourceBean;

    @ToMany(referencedJoinProperty = "mediaId")
    private List<MediaChapterBean> mediaChapterList;

    /**
     * 章节列表url
     */
    private String catalogUrl;

    /**
     * 章节列表如果有分页，这个是分页链接
     */
    private String catalogNextPageUrl;

    protected CollectionMediaBean(Parcel in) {
        this.id = in.readString();
        this.mediaType = in.readInt();
        this.title = in.readString();
        this.author = in.readString();
        this.coverUrl = in.readString();
        this.desc = in.readString();
        this.wordCount = in.readString();
        this.state = in.readString();
        this.category = in.readString();
        this.lastChapter = in.readString();
        this.lastUpdated = in.readString();
        this.lastRead = in.readString();
        this.chaptersCount = in.readInt();
        this.currentChapter = in.readInt();
        this.currentPage = in.readInt();
        this.ifUpdated = in.readByte() != 0;
        this.ifLocal = in.readByte() != 0;
        this.bookSourceUrl = in.readString();
        this.catalogUrl = in.readString();
        this.catalogNextPageUrl = in.readString();
        this.group = in.readString();
        this.varMap = GsonUtils.string2HashMaps(in.readString());
    }

    @Generated(hash = 739227850)
    public CollectionMediaBean(String id, @NotNull Integer mediaType, @NotNull String title, String author, String coverUrl, String desc,
            String wordCount, String state, String category, String lastChapter, String lastUpdated, String lastRead, Integer chaptersCount,
            Integer currentChapter, Integer currentPage, Boolean ifUpdated, Boolean ifLocal, String bookSourceUrl, String group, String diyTitle,
            String diyAuthor, String diyDesc, String diyCoverUrl, Boolean encrypted, HashMap<String, String> varMap, String catalogUrl,
            String catalogNextPageUrl) {
        this.id = id;
        this.mediaType = mediaType;
        this.title = title;
        this.author = author;
        this.coverUrl = coverUrl;
        this.desc = desc;
        this.wordCount = wordCount;
        this.state = state;
        this.category = category;
        this.lastChapter = lastChapter;
        this.lastUpdated = lastUpdated;
        this.lastRead = lastRead;
        this.chaptersCount = chaptersCount;
        this.currentChapter = currentChapter;
        this.currentPage = currentPage;
        this.ifUpdated = ifUpdated;
        this.ifLocal = ifLocal;
        this.bookSourceUrl = bookSourceUrl;
        this.group = group;
        this.diyTitle = diyTitle;
        this.diyAuthor = diyAuthor;
        this.diyDesc = diyDesc;
        this.diyCoverUrl = diyCoverUrl;
        this.encrypted = encrypted;
        this.varMap = varMap;
        this.catalogUrl = catalogUrl;
        this.catalogNextPageUrl = catalogNextPageUrl;
    }

    public CollectionMediaBean() {
    }


    public static final Creator<CollectionMediaBean> CREATOR = new Creator<CollectionMediaBean>() {
        @Override
        public CollectionMediaBean createFromParcel(Parcel source) {
            return new CollectionMediaBean(source);
        }

        @Override
        public CollectionMediaBean[] newArray(int size) {
            return new CollectionMediaBean[size];
        }
    };

    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated(hash = 1062707332)
    private transient CollectionMediaBeanDao myDao;

    @Generated(hash = 1614481777)
    private transient String siteSourceBean__resolvedKey;

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(this.id);
        dest.writeInt(this.mediaType);
        dest.writeString(this.title);
        dest.writeString(this.author);
        dest.writeString(this.coverUrl);
        dest.writeString(this.desc);
        dest.writeString(this.wordCount);
        dest.writeString(this.state);
        dest.writeString(this.category);
        dest.writeString(this.lastChapter);
        dest.writeString(this.lastUpdated);
        dest.writeString(this.lastRead);
        dest.writeInt(this.chaptersCount == null ? 0 : this.chaptersCount);
        dest.writeInt(this.currentChapter == null ? 0 : this.currentChapter);
        dest.writeInt(this.currentPage == null ? 0 : this.currentPage);
        dest.writeByte(this.ifUpdated ? (byte) 1 : (byte) 0);
        dest.writeByte(this.ifLocal ? (byte) 1 : (byte) 0);
        dest.writeString(this.bookSourceUrl);
        dest.writeString(this.catalogUrl);
        dest.writeString(this.catalogNextPageUrl);
        dest.writeString(this.group);
        dest.writeString(GsonUtils.obj2String(this.varMap));
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getWordCount() {
        return wordCount;
    }

    public void setWordCount(String wordCount) {
        this.wordCount = wordCount;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getLastChapter() {
        return lastChapter;
    }

    public void setLastChapter(String lastChapter) {
        this.lastChapter = lastChapter;
    }

    public String getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(String lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public String getBookSourceUrl() {
        return bookSourceUrl;
    }

    public void setBookSourceUrl(String bookSourceUrl) {
        this.bookSourceUrl = bookSourceUrl;
    }

//    public String getVarMapStr() {
//        return varMapStr;
//    }
    public HashMap<String, String> getVarMap() {
        return varMap;
    }
    public void setVarMap(HashMap<String, String> varMap) {
        this.varMap = varMap;
    }

//    public void setVarMapStr(String varMapStr) {
//        this.varMapStr = varMapStr;
//    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getCurrentChapter() {
        return currentChapter;
    }

    public void setCurrentChapter(Integer currentChapter) {
        this.currentChapter = currentChapter;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Boolean getIfUpdated() {
        return ifUpdated;
    }

    public void setIfUpdated(Boolean ifUpdated) {
        this.ifUpdated = ifUpdated;
    }

    public Boolean getIfLocal() {
        return ifLocal;
    }

    public void setIfLocal(Boolean ifLocal) {
        this.ifLocal = ifLocal;
    }


    public String getLastRead() {
        return lastRead;
    }

    public void setLastRead(String lastRead) {
        this.lastRead = lastRead;
    }

    public String getCatalogUrl() {
        return catalogUrl;
    }

    public void setCatalogUrl(String catalogUrl) {
        this.catalogUrl = catalogUrl;
    }

    public String getCatalogNextPageUrl() {
        return catalogNextPageUrl;
    }

    public void setCatalogNextPageUrl(String catalogNextPageUrl) {
        this.catalogNextPageUrl = catalogNextPageUrl;
    }

    @Override
    public int describeContents() {
        return 0;
    }


    public Integer getChaptersCount() {
        return chaptersCount;
    }

    public void setChaptersCount(Integer chaptersCount) {
        this.chaptersCount = chaptersCount;
    }

    public void setMediaChapterList(List<MediaChapterBean> beans) {
        mediaChapterList = beans;
        for (MediaChapterBean bean : mediaChapterList) {
            bean.setMediaId(getId());
        }
    }


    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 926914677)
    public void setSiteSourceBean(SiteSourceBean siteSourceBean) {
        synchronized (this) {
            this.siteSourceBean = siteSourceBean;
            bookSourceUrl = siteSourceBean == null ? null : siteSourceBean.getSiteHostUrl();
            siteSourceBean__resolvedKey = bookSourceUrl;
        }
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 586953159)
    public List<MediaChapterBean> getMediaChapterList() {
        if (mediaChapterList == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            MediaChapterBeanDao targetDao = daoSession.getMediaChapterBeanDao();
            List<MediaChapterBean> mediaChapterListNew = targetDao._queryCollectionMediaBean_MediaChapterList(id);
            synchronized (this) {
                if (mediaChapterList == null) {
                    mediaChapterList = mediaChapterListNew;
                }
            }
        }
        return mediaChapterList;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 899953487)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getCollectionMediaBeanDao() : null;
    }

    public Integer getMediaType() {
        return this.mediaType;
    }

    public void setMediaType(Integer mediaType) {
        this.mediaType = mediaType;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 1523140279)
    public synchronized void resetMediaChapterList() {
        mediaChapterList = null;
    }

    /** To-one relationship, resolved on first access. */
    @Generated(hash = 2001166754)
    public SiteSourceBean getSiteSourceBean() {
        String __key = this.bookSourceUrl;
        if (siteSourceBean__resolvedKey == null || siteSourceBean__resolvedKey != __key) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            SiteSourceBeanDao targetDao = daoSession.getSiteSourceBeanDao();
            SiteSourceBean siteSourceBeanNew = targetDao.load(__key);
            synchronized (this) {
                siteSourceBean = siteSourceBeanNew;
                siteSourceBean__resolvedKey = __key;
            }
        }
        return siteSourceBean;
    }

    public String getGroup() {
        return this.group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getDiyTitle() {
        return this.diyTitle;
    }

    public void setDiyTitle(String diyTitle) {
        this.diyTitle = diyTitle;
    }

    public String getDiyAuthor() {
        return this.diyAuthor;
    }

    public void setDiyAuthor(String diyAuthor) {
        this.diyAuthor = diyAuthor;
    }

    public String getDiyDesc() {
        return this.diyDesc;
    }

    public void setDiyDesc(String diyDesc) {
        this.diyDesc = diyDesc;
    }

    public String getDiyCoverUrl() {
        return this.diyCoverUrl;
    }

    public void setDiyCoverUrl(String diyCoverUrl) {
        this.diyCoverUrl = diyCoverUrl;
    }

    public Boolean getEncrypted() {
        return this.encrypted;
    }

    public void setEncrypted(Boolean encrypted) {
        this.encrypted = encrypted;
    }



}

